package com.company.cloud.pay.modules.mgr.controller;

import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.pay.modules.base.entity.PayServiceConfigInfo;
import com.company.cloud.pay.modules.base.service.PayServiceConfigInfoService;
import com.company.cloud.pay.modules.cache.model.PayServiceConfigModel;
import com.company.cloud.pay.modules.cache.service.PayCacheService;
import com.company.cloud.pay.modules.cache.utils.PayConvertUtils;
import com.company.cloud.pay.modules.mgr.controller.req.PayServiceConfigQuery4PageParam;
import com.company.cloud.pay.modules.mgr.controller.req.PayServiceConfigSaveParam;
import com.company.cloud.pay.modules.mgr.controller.req.PayServiceConfigUpdateStateParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;

/**
 * 支付服务配置
 */
@Authc
@RestController
@RequestMapping("/mgr/pay/serviceConfig")
@Api(tags="Pay_Mgr_支付服务配置管理")
public class PayServiceConfigMgrController {

	@Autowired
	private PayServiceConfigInfoService payServiceConfigInfoService;
	@Autowired
	private PayCacheService payCacheService;
	

	@PostMapping("/query")
	@ApiOperation(value="查询",response=PayServiceConfigModel.class)
	public BaseResult query(@RequestBody PayServiceConfigQuery4PageParam params) {
		IPage<PayServiceConfigInfo> page = payServiceConfigInfoService.page(
				new Page<>(params.getPage(), params.getRows()),
				new QueryWrapper<PayServiceConfigInfo>()
				.eq(StringUtils.hasText(params.getServiceCode()), "service_code", params.getServiceCode())
				.eq(StringUtils.hasText(params.getServiceName()), "service_name",params.getServiceName())
				.eq(StringUtils.hasText(params.getState()), "state", params.getState())
				.eq("rows_state",  EnabledEnum.ENABLED.getCode())
				);
		IPage<PayServiceConfigModel> data = page.convert(new Function<PayServiceConfigInfo, PayServiceConfigModel>() {
			@Override
			public PayServiceConfigModel apply(PayServiceConfigInfo entity) {
				return PayConvertUtils.getPayServiceConfigModel(entity);
			}
		});
		return BaseResult.ok(data);
	}
	
	
	@GetMapping("/get")
	@ApiOperation(value="详细",response=PayServiceConfigModel.class)
	public BaseResult get(@RequestParam("serviceCode")String serviceCode) {
		if(StringUtils.isEmpty(serviceCode)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		PayServiceConfigModel data=payCacheService.getPayServiceConfigByDB(serviceCode);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/save")
	@ApiOperation(value="新增",response=PayServiceConfigModel.class)
	public BaseResult save(@RequestBody PayServiceConfigSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		PayServiceConfigInfo entity=new PayServiceConfigInfo();
		entity.setServiceCode(params.getServiceCode());
		entity.setServiceName(params.getServiceName());
		entity.setSecurityKey(params.getSecurityKey());
		entity.setState(YNEnum.YES.getCode());
		if(params.getServiceRule()!=null) {
			entity.setServiceRule(JSON.toJSONString(params.getServiceRule()));
		}
		entity.setServiceOrderPrefix(params.getServiceOrderPrefix());
		entity.setPayNotifyUrl(params.getPayNotifyUrl());
		entity.setRefundNotifyUrl(params.getRefundNotifyUrl());
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		payServiceConfigInfoService.save(entity);
		
		PayServiceConfigModel data=PayConvertUtils.getPayServiceConfigModel(entity);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/update")
	@ApiOperation(value="修改",response = PayServiceConfigModel.class)
	public BaseResult update(@RequestBody PayServiceConfigSaveParam params) {
		ValidatorUtils.validate(params);
		PayServiceConfigInfo entity= payServiceConfigInfoService.getById(params.getServiceCode());
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setServiceName(params.getServiceName());
		entity.setSecurityKey(params.getSecurityKey());
		entity.setState(YNEnum.YES.getCode());
		if(params.getServiceRule()!=null) {
			entity.setServiceRule(JSON.toJSONString(params.getServiceRule()));
		}
		entity.setServiceOrderPrefix(params.getServiceOrderPrefix());
		entity.setPayNotifyUrl(params.getPayNotifyUrl());
		entity.setRefundNotifyUrl(params.getRefundNotifyUrl());
		entity.addEditParam(userInfo);
		payServiceConfigInfoService.updateById(entity);
		PayServiceConfigModel data=PayConvertUtils.getPayServiceConfigModel(entity);
		payCacheService.removePayServiceConfigByCache(entity.getServiceCode());
		return BaseResult.ok(data);
	}
	
	
	@PostMapping("/updateState")
	@ApiOperation(value="修改状态",response = BaseResult.class)
	public BaseResult updateState(@RequestBody PayServiceConfigUpdateStateParam params) {
		ValidatorUtils.validate(params);
		PayServiceConfigInfo entity= payServiceConfigInfoService.getById(params.getServiceCode());
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(entity.getState().equals(params.getState())) {
			return BaseResult.ok();
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setState(params.getState());
		entity.addEditParam(userInfo);
		payServiceConfigInfoService.updateById(entity);
		payCacheService.removePayServiceConfigByCache(entity.getServiceCode());
		return BaseResult.ok();
	}
	
	
	@GetMapping("/delete")
	@ApiOperation(value="删除")
	public BaseResult delete(@RequestParam("serviceCode")String serviceCode) {
		if(StringUtils.isEmpty(serviceCode)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		PayServiceConfigInfo entity= payServiceConfigInfoService.getById(serviceCode);
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setRowsState(EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		payServiceConfigInfoService.updateById(entity);
		payCacheService.removePayServiceConfigByCache(entity.getServiceCode());
		return BaseResult.ok();
	}
	
	
	
}
